﻿using System;
using System.Collections.Generic;
using Assist;

namespace HYRElectric.Calculate
{
	internal class StuWallRef
	{
		public StuWallRef(double dVal1, double dVal2)
		{
			this.dCeiling = dVal1;
			this.dWall = dVal2;
		}

		public double Cal()
		{
			InterpolationType interpolationType = InterpolationType.eIn;
			double num = 0.0;
			double num2 = 0.0;
			double result = 0.0;
			if (this.FindEqualValue())
			{
				num = FindUseFactor.m_stuUseFactor.dFloor;
				num2 = FindUseFactor.m_stuUseFactor.dFloor;
			}
			else if (!this.FindClosedValue(ref num, ref num2, ref interpolationType))
			{
				return result;
			}
			if (GeoUtil.IsEqual(num, num2))
			{
				this.left = new StuFloorRef(this.dCeiling, this.dWall, num);
				result = this.left.Cal();
			}
			else
			{
				this.left = new StuFloorRef(this.dCeiling, this.dWall, num);
				this.right = new StuFloorRef(this.dCeiling, this.dWall, num2);
				double num3 = this.left.Cal();
				double num4 = this.right.Cal();
				result = num3 + (num4 - num3) * (FindUseFactor.m_stuUseFactor.dFloor - num) / (num2 - num);
			}
			return result;
		}

		private bool FindEqualValue()
		{
			return FindUseFactor.m_arUseFactor.Find(new Predicate<StuUseFactor>(this.Equals)) != null;
		}

		private bool FindClosedValue(ref double dLeft, ref double dRight, ref InterpolationType InterType)
		{
			double dFloor = FindUseFactor.m_stuUseFactor.dFloor;
			List<double> list = new List<double>();
			StuWallRef.FloorRefComparer comparer = new StuWallRef.FloorRefComparer();
			FindUseFactor.m_arUseFactor.Sort(comparer);
			foreach (StuUseFactor stuUseFactor in FindUseFactor.m_arUseFactor)
			{
				if (GeoUtil.IsEqual(stuUseFactor.dCeiling, this.dCeiling) && GeoUtil.IsEqual(stuUseFactor.dWall, this.dWall))
				{
					list.Add(stuUseFactor.dFloor);
				}
			}
			return FindValue.FindClosedValue(list, dFloor, ref dLeft, ref dRight, ref InterType);
		}

		private bool Equals(StuUseFactor u)
		{
			return GeoUtil.IsEqual(u.dCeiling, this.dCeiling) && GeoUtil.IsEqual(u.dWall, this.dWall) && GeoUtil.IsEqual(u.dFloor, FindUseFactor.m_stuUseFactor.dFloor);
		}

		private double dCeiling;

		private double dWall;

		private StuFloorRef left;

		private StuFloorRef right;

		public class FloorRefComparer : IComparer<StuUseFactor>
		{
			int IComparer<StuUseFactor>.Compare(StuUseFactor first, StuUseFactor second)
			{
				if (first == null && second == null)
				{
					return 0;
				}
				if (first != null && second == null)
				{
					return 1;
				}
				if (first == null && second != null)
				{
					return -1;
				}
				return first.dFloor.CompareTo(second.dFloor);
			}
		}
	}
}
